package com.example.cd5160866.urlapp;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.cd5160866.urlapp.models2.ItemViewId;
import com.example.cd5160866.urlapp.models2.ModelRecyclerAdapterV2;
import com.example.cd5160866.urlapp.view.AssginScrollView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


public class Demo7ScrollerActivity extends AppCompatActivity {
    RecyclerView recyclerView;

    private List<String> datas;

    private Handler handler;

    @InjectView(R.id.scrollview)
    AssginScrollView scrollView;

    @InjectView(R.id.llay_group)
    LinearLayout llayGroup;
    @InjectView(R.id.llay_sroll)
    LinearLayout llaySroll;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add:
//                adapter.add(1, "http://a1.att.hudong.com/28/57/20300534506289144593571229462_s.jpg", handler);
                break;
            case R.id.delete:
//                adapter.remove(0, handler);
                break;
        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_scroller);
        ButterKnife.inject(this);
//        handler = new Handler();
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//        recyclerView = (RecyclerView) findViewById(R.id.rv_demo1);
//        LinearLayoutManager manager = new GridLayoutManager(this, 4);
//        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
//        recyclerView.setLayoutManager(manager);
//        initData();
//        adapter = new ModelRecyclerAdapterV2(this, MyViewHolder.class, datas);
//        recyclerView.setAdapter(adapter);
        initData();
        addItme();
//


    }

    @OnClick(R.id.btn_test1)
    void test1() {
//        llayGroup.scrollBy(-10, 0);
        scrollView.smoothScrollTo(0,0);
    }

    @OnClick(R.id.btn_test2)
    void test2() {
//        llayGroup.scrollTo(40, 0);
        Log.d("qw", scrollView.getCurrentY() + " currentY");
        Log.d("qw", llaySroll.getChildAt(0).getLeft() + " firstLeft");
        Log.d("qw", llaySroll.getChildAt(0).getRight() + " firstRight");
        Log.d("qw", llaySroll.getChildAt(0).getX() + " firstx");
    }

    //    private void smothScrollView(final View view, final int xOffset, int yOffset) {
//        final float[] value = {0};
//        ValueAnimator animator = ValueAnimator.ofFloat(0, 1).setDuration(500);
//        animator.setInterpolator(new AccelerateDecelerateInterpolator());
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                 value[0] = (float) animation.getAnimatedValue()-value[0];
//                Log.d("qw","value"+value[0]);
////                formerDistance[0] = (int) (xOffset * value) - formerDistance[0];
////                view.scrollBy(formerDistance[0], 0);
//
//            }
//        });
//        animator.start();
//    }
    void addItme() {
        for (String data : datas) {
            View view = LayoutInflater.from(Demo7ScrollerActivity.this).inflate(R.layout.item_1, llaySroll, false);
            llaySroll.addView(view);
//            View view = View.inflate(llaySroll.getContext(),R.layout.item_1,null);
//            llaySroll.addView(view);
            ImageView image = (ImageView) view.findViewById(R.id.iv_item1);
            Picasso.with(image.getContext()).load(data).into(image);
        }
    }


    ModelRecyclerAdapterV2 adapter;

    @ItemViewId(R.layout.item_1)
    public static class MyViewHolder extends ModelRecyclerAdapterV2.ModelViewHolder<String> {
        @InjectView(R.id.iv_item1)
        ImageView imageView;

        @OnClick(R.id.iv_item1)
        void onclick() {
            Toast.makeText(imageView.getContext(), position + "", Toast.LENGTH_SHORT).show();
        }

        public int position;


        public MyViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void update(String item, ModelRecyclerAdapterV2 adapter, Context context, int positon) {
            this.position = positon;
            Picasso.with(context).load(item).into(imageView);
        }
    }

    void initData() {
        datas = new ArrayList<>();
        datas.add("http://img3.imgtn.bdimg.com/it/u=2546610023,3120506294&fm=11&gp=0.jpg");
        datas.add("http://pic32.nipic.com/20130829/12906030_124355855000_2.png");
        datas.add("http://e.hiphotos.baidu.com/image/pic/item/14ce36d3d539b600be63e95eed50352ac75cb7ae.jpg");
        datas.add("http://b.hiphotos.baidu.com/image/pic/item/f703738da9773912825f6388fc198618377ae2da.jpg");
        datas.add("http://img5.duitang.com/uploads/item/201408/19/20140819091649_zNBkz.jpeg");
        datas.add("http://img3.100bt.com/upload/ttq/20130413/1365830891087.jpg");
        datas.add("http://img2.duitang.com/uploads/item/201201/16/20120116004745_THihZ.jpg");
        datas.add("http://www.xiami.com/images/collect/349/49/13846349_1367639271.jpg");
        datas.add("http://a1.att.hudong.com/28/57/20300534506289144593571229462_s.jpg");
        datas.add("http://g.hiphotos.baidu.com/image/h%3D200/sign=ccb17e18d03f8794ccff4f2ee21a0ead/a8773912b31bb051a6ed2e88327adab44bede041.jpg");
    }

}
